package collectdemo13.set.hashset;

import java.util.Iterator;
import java.util.HashSet;
import java.util.Set;
class Dog
{
	private String name = null;
	private int age = 0;
	
	public Dog(String name, int age)
	{
		super();
		this.name = name;
		this.age = age;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public int getAge()
	{
		return age;
	}
	public void setAge(int age)
	{
		this.age = age;
	}
	@Override
	public int hashCode()
	{
		return this.name.hashCode()*this.age;
	}
	public boolean equals(Object obj)
	{
		if(this == obj)
		{
			return true;
		}
		else
		{
			if(!(obj instanceof Dog))
			{
				return false;
			}
			Dog dog = (Dog)obj;
			if(dog.getAge() == this.getAge() && dog.getName().equals(this.getName()))
			{
				return true;
			}
			else
			{
				return false;
			}	
		}
	}
	@Override
	public String toString()
	{
		return "Dog [age=" + age + ", name=" + name + ", getAge()=" + getAge() + ", getName()=" + getName();
	}
	
	
}

public class DelDuplicatedHashSetDemo02
{
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		// TODO Auto-generated method stub
		Set<Dog> set = new HashSet<Dog>();
		Dog [] per = new Dog[10];
		for(int i = 0;i<10;i++)
		{
			per[i] = new Dog("i"+i,i);
			set.add(per[i]);
		}
		set.add(new Dog("张三",30));
		set.add(new Dog("张三",30));
		set.add(new Dog("李四",30));
		set.add(new Dog("王五",20));
		Iterator<Dog> it =  set.iterator();
		while(it.hasNext())
		{
			Dog temp = it.next();
			System.out.println("增加后的Dog是："+temp);
		}
	}

}
